//
//  IBGiftBox.m
//  烟花特效
//
//  Created by apple on 15/7/10.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "IBGiftBox.h"
#import "IBImageExplodeImageView.h"

@interface IBGiftBox()
@property (strong, nonatomic)CABasicAnimation *rotationAnimation;//处理动画效果
@property (strong, nonatomic)UIView *boxImage;//礼物视图地图(汽车,钻戒)
@property (strong, nonatomic)UIView *carView;
@end
@implementation IBGiftBox

-(instancetype)initWithGiftView:(UIView *)view frame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
//        self.backgroundColor = [UIColor blueColor];
        view.frame = CGRectMake(0, 0, 50, 50);
        self.boxImage = view;
    }
    return self;
}

-(void)startAnimationTime:(float)time Completed:(animationCompleted)completed{
    
        //礼盒下边
        IBImageExplodeImageView *downImage = [[IBImageExplodeImageView alloc] initWithFrame:CGRectMake(self.frame.size.width / 2 - 238 /4, self.frame.size.height / 2, 238 /2, 251 / 2)];
        downImage.image = [UIImage imageNamed:@"礼盒下身.png"];
        [self addSubview:downImage];
    
        //汽车
        self.carView = [[UIView alloc]initWithFrame:CGRectMake(self.frame.size.width / 2 - 238 /4 + 29, self.frame.size.height / 2 + 50, 50, 50)];

        [self.carView addSubview:self.boxImage];
        [self addSubview:self.carView];
    
        //盒子外侧
        IBImageExplodeImageView * imageViewWaice = [[IBImageExplodeImageView alloc] initWithFrame:CGRectMake(self.frame.size.width / 2 - 238 /4 + 9, self.frame.size.height / 2 + 42, 202 / 2, 166 / 2)];
        imageViewWaice.image = [UIImage imageNamed:@"礼盒下身外侧.png"];
        [self addSubview:imageViewWaice];
    
        //礼盒盖子
        IBImageExplodeImageView *upImage = [[IBImageExplodeImageView alloc] initWithFrame:CGRectMake(self.frame.size.width / 2 - 238 /4, self.frame.size.height / 2, 238 /2, 251 / 2)];
        upImage.image = [UIImage imageNamed:@"礼盒盖子.png"];
        [self addSubview:upImage];
    
//    移动动画
        __weak IBGiftBox *weakSelf = self;
        [UIView animateWithDuration:time animations:^{
            //图片动画
            CGFloat M = 3.1415;
            weakSelf.rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
//            weakSelf.rotationAnimation.delegate = weakSelf;
            weakSelf.rotationAnimation.fromValue = [NSNumber numberWithFloat:0];
            weakSelf.rotationAnimation.toValue = [NSNumber numberWithFloat:-M / 2];
            weakSelf.rotationAnimation.duration = time;
            weakSelf.rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
            [weakSelf.rotationAnimation setValue:@"rotationAnimation" forKey:@"MyAnimationType"];
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:time];
            CGAffineTransform transform = CGAffineTransformMakeRotation(1.5*M);
            upImage.transform = transform;
            [UIView commitAnimations];
            
            //盖子移动
            upImage.frame = CGRectMake(- upImage.frame.size.width / 2 , -upImage.frame.size.width / 3, 238 /2, 251 / 2);
            [upImage.layer addAnimation:weakSelf.rotationAnimation forKey:@"rotationAnimation"];

            //汽车移动
            weakSelf.boxImage.frame = CGRectMake( CGRectGetMidY(upImage.frame) - upImage.frame.size.width / 2, -upImage.frame.size.height, 150, 150);
            NSLog(@"动画移动完成");
        }];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((time - 1) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
             NSLog(@"爆破完成");
            [upImage explode];
            [imageViewWaice explode];
            [downImage explode];
            completed();
            
        });
    }



//防止bug不能删
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{

}


-(void)dealloc{
    NSLog(@"释放");
//    self.rotationAnimation.delegate = nil;
}
@end
